package com.su.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.su.tools.DaoSupport1;
import com.su.tools.PageData;

import net.sf.json.JSONObject;

@Service("dynamicService")
public class DynamicService {
	
	@Resource(name = "daoSupport1")
	private DaoSupport1 dao;
	private JSONObject json = new JSONObject();
	private PageData retPd;
	private List<PageData> retListPd;

	public JSONObject invoke(PageData pd) throws Exception {

		if (pd.getString("params").equals("DC101")) {
			this.DC101(pd);
		} else if (pd.getString("params").equals("DC102")) {
			this.DC102(pd);
		} else if (pd.getString("params").equals("DC103")) {
			this.DC103(pd);
		} else if (pd.getString("params").equals("DC104")) {
			this.DC104(pd);
		} else if (pd.getString("params").equals("DC105")) {
			this.DC105(pd);
		} else if (pd.getString("params").equals("DC106")) {
			this.DC106(pd);
		} else if (pd.getString("params").equals("DC107")) {
			this.DC107(pd);
		} else if (pd.getString("params").equals("DC108")) {
			this.DC108(pd);
		} else if (pd.getString("params").equals("DC109")) {
			this.DC109(pd);
		} else if (pd.getString("params").equals("DC110")) {
			this.DC110(pd);
		} else {
			json.put("code", "100");
			json.put("msg", "没有找到该业务代码");
			return json;
		}
		return json;
	}
	
	/**
	 * 获取动态列表
	 * 
	 * @param pd
	 */
	private void DC101(PageData pd) throws Exception {
		retListPd = dao.getList("DynamicMapper.countDynamic", pd);
		int total = retListPd.size();
		retListPd = dao.getList("DynamicMapper.getDynamicList", pd);
		json.put("total", total);
		json.put("rows", retListPd);
	}
	
	/**
	 * 获取动态状态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC102(PageData pd) throws Exception {
		retListPd = dao.getList("DynamicMapper.getStatus", pd);
		json.put("status", retListPd);
		retListPd = dao.getList("DynamicMapper.getDynamicCate", pd);//获取动态类别
		json.put("dynamicCate", retListPd);
	}

	/**
	 * 保存动态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC103(PageData pd) throws Exception {
		String action = pd.getString("action");
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNow = sdf.format(d);
		pd.put("CREATE_DATE", dateNow);
		HttpServletRequest request = pd.getRequest();
		HttpSession session = request.getSession();
		String CZRY = session.getAttribute("session_user").toString();
		pd.put("CZRY", CZRY);
		int rows = 0;
		if (action.equals("add")) {
			rows = dao.insert("DynamicMapper.insertDynamic", pd);
		} else {
			rows = dao.update("DynamicMapper.updateDynamic", pd);
		}
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}

	/**
	 * 获取动态信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC104(PageData pd) throws Exception {
		retPd = dao.getOne("DynamicMapper.getDynamicOne", pd);
		if (retPd!=null) {
			json.put("status", "true");
			json.put("result", retPd);
		} else {
			json.put("status", "false");
			json.put("result", retPd);
		}
	}
	
	/**
	 * 删除动态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC105(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] codeArray = codeList.split(",");
		int rows = dao.delete("DynamicMapper.delDynamic", codeArray);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}

	/**
	 * 修改动态状态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC106(PageData pd) throws Exception {
		int rows=dao.update("DynamicMapper.updateStatus", pd);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	
	/**
	 * 审核动态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC107(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] codeArray = codeList.split(",");
		int rows=dao.update("DynamicMapper.reviewDynamic", codeArray);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	
	/**
	 * 发布动态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC108(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] codeArray = codeList.split(",");
		int rows=dao.update("DynamicMapper.releaseDynamic", codeArray);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	
	/**
	 * 停止动态
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC109(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] codeArray = codeList.split(",");
		int rows=dao.update("DynamicMapper.stopDynamic", codeArray);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	/**
	 * 获取动态编码
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DC110(PageData pd) throws Exception {
		retPd = dao.getOne("DynamicMapper.getMaxDynamicCode", pd);
		String MAX_CODE="";
		if (retPd == null) {
			MAX_CODE = "1";
		} else {
			MAX_CODE = retPd.get("MAX_CODE").toString();
		}
		json.put("MAX_CODE", MAX_CODE);
	}

}
